package com.hzf.config.admin.controller.manage;

import com.baidu.unbiz.fluentvalidator.ComplexResult;
import com.baidu.unbiz.fluentvalidator.FluentValidator;
import com.baidu.unbiz.fluentvalidator.ResultCollectors;
import com.hzf.common.base.BaseController;
import com.hzf.common.util.PropertiesFileUtil;
import com.hzf.common.util.StringUtil;
import com.hzf.common.validator.LengthValidator;
import com.hzf.common.validator.NotNullValidator;
import com.hzf.config.dao.model.ConfigPtanli;
import com.hzf.config.dao.model.ConfigPtanliExample;
import com.hzf.config.dao.model.ConfigStyle;
import com.hzf.config.dao.model.ConfigStyleExample;
import com.hzf.config.rpc.api.ConfigPtanliService;
import com.hzf.config.rpc.api.ConfigStyleService;
import com.hzf.config.rpc.common.constant.ConfigResult;
import com.hzf.config.rpc.common.constant.ConfigResultConstant;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/manage/anli")
public class ConfigPtanliController extends BaseController {
    private static Logger _log = LoggerFactory.getLogger(ConfigPtanliController.class);
    private static final String HZF_OSS_ALIYUN_OSS_POLICY = PropertiesFileUtil.getInstance("hzf-oss-client").get("hzf.oss.aliyun.oss.policy");

    @Autowired
    private ConfigPtanliService configPtanliService;
    @Autowired
    private ConfigStyleService configStyleService;
    @RequestMapping(value="/index",method = RequestMethod.GET)
    @RequiresPermissions("anli:config:read")
    @ApiOperation(value = "案例设置首页")
    public String index(){
        return "/manage/anli/index.jsp";
    }
    @RequestMapping(value="/list",method = RequestMethod.GET)
    @ApiOperation(value = "案例设置列表")
    @RequiresPermissions("anli:config:read")
    @ResponseBody
    public Object list(
            @RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            @RequestParam(required = false, defaultValue = "", value = "search") String search,
            @RequestParam(required = false, value = "sort") String sort,
            @RequestParam(required = false, value = "order") String order) {
        ConfigPtanliExample ConfigPtanliExample = new ConfigPtanliExample();
        if(!StringUtils.isBlank(sort)&&!StringUtils.isBlank(order)){
            ConfigPtanliExample.setOrderByClause(StringUtil.humpToLine(sort) + " " + order);
        }
        List<ConfigPtanli> rows = configPtanliService.selectByExampleForOffsetPage(ConfigPtanliExample,offset,limit);
        long total = configPtanliService.countByExample(ConfigPtanliExample);
        Map<String, Object> result = new HashMap<>();
        result.put("rows", rows);
        result.put("total", total);
        return result;
    }
    @ApiOperation(value = "新增地区")
    @RequiresPermissions("anli:config:create")
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String create(Model model) {

        ConfigStyleExample styleExample = new ConfigStyleExample();
        List<ConfigStyle> rows = configStyleService.selectByExample(styleExample);
        model.addAttribute("HZF_OSS_ALIYUN_OSS_POLICY", HZF_OSS_ALIYUN_OSS_POLICY);

        model.addAttribute("types",rows);
        return "/manage/anli/create.jsp";
    }
    @ApiOperation(value = "新增地区")
    @RequiresPermissions("anli:config:create")
    @ResponseBody
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public Object create(ConfigPtanli ConfigPtanli) {


        int count = configPtanliService.insert(ConfigPtanli);

        return new ConfigResult(ConfigResultConstant.SUCCESS, count);
    }
    @ApiOperation(value = "删除地区")
    @RequiresPermissions("anli:config:delete")
    @RequestMapping(value = "/delete/{ids}",method = RequestMethod.GET)
    @ResponseBody
    public Object delete(@PathVariable("ids") String ids) {
        int count = configPtanliService.deleteByPrimaryKeys(ids);
        return new ConfigResult(ConfigResultConstant.SUCCESS, count);
    }
    @ApiOperation(value = "修改案例")
    @RequiresPermissions("anli:config:update")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
    public String update(@PathVariable("id") int id, ModelMap modelMap) {
        ConfigPtanli ConfigPtanli = configPtanliService.selectByPrimaryKey(id);
        modelMap.put("ConfigPtanli", ConfigPtanli);
        System.out.println("返回了");
        return "/manage/anli/update.jsp";
    }

    @ApiOperation(value = "修改用户")
    @RequiresPermissions("anli:config:update")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Object update(@PathVariable("id") Integer id, ConfigPtanli ConfigPtanli) {

        ConfigPtanli.setId(id);
        int count = configPtanliService.updateByPrimaryKeySelective(ConfigPtanli);
        return new ConfigResult(ConfigResultConstant.SUCCESS, count);
    }
}
